module Main where

import Network.Socket
import Control.Concurrent

main :: IO ()
main = do
    putStrLn "111111111111111"
    sock <- socket AF_INET Stream 0
    putStrLn "2222222222222222222222"
    bindSocket sock (SockAddrInet 4242 iNADDR_ANY)
    putStrLn "3333333333333"
    listen sock 10240
    mainLoop sock

mainLoop :: Socket -> IO ()
mainLoop sock = do
    putStrLn "mainLoop:enter"

    conn <- accept sock
    forkIO $ runConn conn
    mainLoop sock

runConn :: (Socket, SockAddr) -> IO ()
runConn (sock, tcp) = do
    putStrLn "start recv"
    sms<-recv sock 1024
    peeraddr <- getPeerName sock
    putStrLn "after recv"
    putStrLn sms
    runConn (sock, tcp)
